//
//  YYFrequentlyUseView.swift
//  STMusic
//
//  Created by stoneobs on 17/4/19.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

import UIKit
import AVFoundation
//包括头像，名字，人类型（原创音乐人），发布时间，点赞按钮
class YYFrequentlyUserCellHeader: UIView {
    //为了避免大部分修改，所以弄了三个model
    //乐评模型,height 50
    var model:YYRecommendModel?{
        didSet{
            
            self.iconButton?.sd_setImage(with: URL.init(string: (self.model?.user?.avatar)!), for: .normal, placeholderImage: PeopleImage, options: .retryFailed);
            
            self.nickNameLabel?.text = self.model?.user?.nickname;
            let nickNameStr:NSString = NSString.init(string: (self.model?.user?.nickname)!)
            self.nickNameLabel?.width = nickNameStr.widthWithheight(15, font: 15);
            self.personTypeLabel?.left = (self.nickNameLabel?.right)! + 5;
            self.personTypeLabel?.text = self.model?.user?.identity_tag;
            
            let date:NSDate = NSDate.init(timeIntervalSince1970: TimeInterval((self.model?.file?.create_time)!)!)
            
            self.timeLabel?.text = date.yyyyMMddhhmmssWithDate();
            
            
            
            
        }
    }
    //评论模型
    var commentModel:YYCommentModel?{
    
        didSet{
            self.iconButton?.sd_setImage(with: URL.init(string: (self.commentModel?.user?.avatar)!), for: .normal, placeholderImage: PeopleImage, options: .retryFailed);
            

            
            
            self.nickNameLabel?.text = self.commentModel?.user?.nickname;
            let nickNameStr:NSString = NSString.init(string: (self.commentModel?.user?.nickname)!)
            self.nickNameLabel?.width = nickNameStr.widthWithheight(15, font: 15);
            self.personTypeLabel?.left = (self.nickNameLabel?.right)! + 5;
            self.personTypeLabel?.text = self.commentModel?.user?.identity_tag;
            
            let date:NSDate = NSDate.init(timeIntervalSince1970: TimeInterval((self.commentModel?.create_time)!)!)
            
            self.timeLabel?.text = date.yyyyMMddhhmmssWithDate();
        }
    }
    
    var userModel:YYuserModel?{
        didSet{
        
            self.iconButton?.sd_setImage(with: URL.init(string: (userModel?.avatar)!), for: .normal, placeholderImage: PeopleImage, options: .retryFailed);
            
            
            
            
            self.nickNameLabel?.text = userModel?.nickname;
            let nickNameStr:NSString = NSString.init(string: (userModel?.nickname)!)
            self.nickNameLabel?.width = nickNameStr.widthWithheight(15, font: 15);
            self.personTypeLabel?.left = (self.nickNameLabel?.right)! + 5;
            self.personTypeLabel?.text = userModel?.identity_tag;
            

        
        }
    }
    
    //UI
    var iconButton:STButton?
    
    var nickNameLabel:STLabel?
    
    var personTypeLabel:STLabel?
    
    var goodButton:STButton?
    
    var timeLabel:STLabel?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initSubview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initSubview() {
        
        self.iconButton = STButton.init(frame: .init(x: 15, y: 10, width: 40, height: 40), title: "", titleColor: nil, titleFont: 0, cornerRadius: 20, backgroundColor: .white, backgroundImage: PeopleImage, image: nil);
        self.addSubview(self.iconButton!);
        self.iconButton?.showRoundShadow();
        
        self.nickNameLabel = STLabel.init(frame: CGRect.init(x: 10 + (iconButton?.right)!, y: (iconButton?.top)!, width: 80, height: 15), text: "stoneobs", textColor: FirstTextColor, font: 15, isSizetoFit: false, textAlignment: .left);
        self.addSubview(self.nickNameLabel!);
        
        self.personTypeLabel = STLabel.init(frame: CGRect.init(x: 10 + (nickNameLabel?.right)!, y: (iconButton?.top)!, width: 80, height: 13), text: "音乐人", textColor: SecendTextColor, font: 13, isSizetoFit: false, textAlignment: .left);
        self.personTypeLabel?.centerY = (self.nickNameLabel?.centerY)!;
        self.addSubview(self.personTypeLabel!);
        
        self.goodButton = STButton.init(frame: .init(x:SCREEN_WIDTH -  15 - 20, y: 10, width: 20, height: 20), title: "1", titleColor: FirstTextColor , titleFont: 13 , cornerRadius: 0, backgroundColor: .white, backgroundImage: UIImage.init(named: "btn_w_zan_s"), image: nil);
        self.goodButton?.letImageViewAsright(1);
        self.addSubview(self.iconButton!);
        
        self.timeLabel = STLabel.init(frame: CGRect.init(x: 10 + (iconButton?.right)!, y: (nickNameLabel?.bottom)! + 5, width: 180, height: 13), text: "2016/1/21", textColor: ThirdTextColor, font: 13, isSizetoFit: false, textAlignment: .left);
        
        self.addSubview(self.timeLabel!);

    }


}
//乐评控件
class YYFrequentlyMusicCommentView: UIView {
    
    var model:YYRecommendModel?{
        didSet{
            let gesTap = UITapGestureRecognizer.init(target: self, action: #selector(imageViewAction(tapGes:)));
            self.addGestureRecognizer(gesTap);
            
            self.numLabel?.text = "收听" + (self.model?.file?.listen_num)!;
            
            self.timeLabel?.text = (self.model?.file?.audio_long)! + "‘’";
            

            if self.model?.file?.isPlaying  == true{
                self.imageView?.startAnimating();
            }else{
                self.imageView?.stopAnimating();
            }
            
            

        }
    }
    //UI

    override var  frame: CGRect{
    
        didSet{
            //重新计算
            self.calculateFrame();

        }
    }
    var roundBackView:UIView?
    
    var backView:UIView?
    
    var imageView:UIImageView?
    
    var numLabel:STLabel?

    var timeLabel:STLabel?
    

    
    
    var imageViewActionHandle:(_ handleImageView:UIView) -> Void = {_ in };
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initSubview()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initSubview() {
        
        self.backgroundColor = .white;
        roundBackView = UIView.init(frame: .init(x: 0, y: 0, width: self.height  , height: self.height))

        roundBackView?.backgroundColor = YYThemeStyleManger.defultManger.backeGroundColor;
        self.addSubview(roundBackView!);
        
        backView = UIView.init(frame: .init(x: 0, y: 0, width: self.width - self.height / 2, height: self.height));
        backView?.backgroundColor = YYThemeStyleManger.defultManger.backeGroundColor;
        backView?.layer.cornerRadius = 2;
        backView?.clipsToBounds = true;
        self.addSubview(backView!);
        
        let label = STLabel.init(frame: .init(x: 15, y: 0, width: 40, height: 15), text: "乐评", textColor: .white, font: 14, isSizetoFit: false, textAlignment: .left);
        label?.centerY = self.height / 2;
        self.addSubview(label!);
        
        
        
        self.numLabel = STLabel.init(frame: .init(x: (label?.right)! + 10, y: 0, width: 150, height: 10), text: "收听1000", textColor: .white, font: 10, isSizetoFit: false, textAlignment: .left);
        self.numLabel?.centerY = self.height / 2;
        self.addSubview(self.numLabel!);
        
        imageView = UIImageView.init(frame: .init(x: 0, y: 0, width: 16, height: 16));
        imageView?.right = self.height / 2;
        imageView?.centerY = self.height / 2;
        imageView?.animationImages = [UIImage.init(named: "img_voice1")!,UIImage.init(named: "img_voice2")!,UIImage.init(named: "img_voice3")!];
        imageView?.isUserInteractionEnabled = true;
        imageView?.image = UIImage.init(named: "img_voice3")!;
        imageView?.animationDuration = 1.5;
        self.addSubview(imageView!);
        
        
        self.timeLabel = STLabel.init(frame: .init(x: (imageView?.left)! - 2 - 40  , y: 0, width: 40, height: 10), text: "99" + "‘", textColor: .white, font: 10, isSizetoFit: false, textAlignment: .right);
        self.numLabel?.centerY = self.height / 2;
        self.addSubview(self.timeLabel!);
        
        self.calculateFrame();
        
    }
    func calculateFrame() -> Void {
        roundBackView?.size = .init(width: self.height, height: self.height);
        roundBackView?.right = self.width;
        roundBackView?.layer.cornerRadius = self.height / 2;
        roundBackView?.clipsToBounds = true;
        
        backView?.width = self.width - self.height / 2;
        backView?.left = 0;
        backView?.top = 0;
        
        imageView?.right = (backView?.width)!;
        self.timeLabel?.right = (imageView?.left)! - 3;
        self.timeLabel?.centerY = self.height / 2;
        
       
    }
    func imageViewAction(tapGes:UITapGestureRecognizer) -> Void {
        
        self.imageViewActionHandle(tapGes.view!);
    }


    
}
